# -*- coding: utf-8 -*-
import random

import scrapy
from lxml import etree
from soufang.items import SoufangItem


class SearchhouseSpider(scrapy.Spider):
    name = 'searchhouse'
    # allowed_domains = ['0722fc.com']
    start_urls = ['https://www.0722fc.com']

    for pn in range(1,2):
        url = 'http://www.0722fc.com/index/index.php?caid=3&addno=1&ccid1=207&page=%s' % pn
        start_urls.append(url)

    def parse(self, response):

        urls = response.xpath('//div[@class="info housetitle"]//a[@class="fz14 fzhoues"]/@href').extract()
        print(urls)
        if urls:
            for url in urls:
                yield response.follow(url, callback=self.parse_item)

    def parse_item(self, response):
        items = {}
        item = SoufangItem()
        if response:
            # item['id'] = str(random.randint(1, 100000))
            item['total_price'] = response.xpath(
                '//div[@class="house-info mb20"]//span[@class="fz22 eszj"]/text()').extract_first()
            item['houseing_pattern'] = response.xpath(
                '//div[@class="house-info mb20"]/ul/li[2]/i/text()').extract_first()
            item['unit_price'] = response.xpath('//div[@class="house-info mb20"]/ul/li[4]/i/text()').extract_first()
            item['covered_area'] = response.xpath('//div[@class="house-info mb20"]/ul/li[2]/i/text()').extract_first()
            item['construction_time'] = response.xpath(
                '//div[@class="house-info mb20"]/ul/li[11]/i/text()').extract_first()
            item['decorate_class'] = response.xpath('//div[@class="house-info mb20"]/ul/li[7]/i/text()').extract_first()
            item['house_category'] = response.xpath('//div[@class="house-info mb20"]/ul/li[8]/i/text()').extract_first()
            item['orientation'] = response.xpath('//div[@class="house-info mb20"]/ul/li[10]/i/text()').extract_first()
            item['address'] = response.xpath('//div[@class="house-info mb20"]/ul/li[12]/i/text()').extract_first()
            item['floor'] = response.xpath('//div[@class="house-info mb20"]/ul/li[9]/i/text()').extract_first()
            print(item)
            items.append(item)
            return items
